package csc.traning.wpsj.servlet.upload;


import java.io.File;

public class FolderProcess {

    int totalFolder = 0;
    int totalFile = 0;

    public static void main(String args[]) {
        String folder = "C:/eclipse";
        try {
        	FolderProcess size = new FolderProcess();
            long fileSizeByte = size.getFileSize(new File(folder));
            System.out.println("Folder Size: " + fileSizeByte + " Bytes");
            //System.out.println("Total Number of Folders: "+ size.getTotalFolder());
           // System.out.println("Total Number of Files: " + size.getTotalFile());
            size.getFileInFolder(new File(folder));
        } catch (Exception e) {}
    }

    public long getFileSize(File folder) {
        totalFolder++;
        //System.out.println("Folder: " + folder.getName());
        long foldersize = 0;
        if(!folder.exists()){
        	folder.mkdir();
        	return 0;
        	}
        File[] filelist = folder.listFiles();
        if(filelist.length==0)
        	return 0;
        for (int i = 0; i < filelist.length; i++) {
            if (filelist[i].isDirectory()) {
                foldersize += getFileSize(filelist[i]);
            } else {
                totalFile++;
                foldersize += filelist[i].length();
            }
        }
        return foldersize;
        
    }
    
    public void getFileInFolder(File folder){
    	File[] filelist=folder.listFiles();
    	for(int i=0;i<filelist.length;i++){
    		if(filelist[i].isDirectory()){
    			System.out.println("Folder: "+filelist[i]);
    		}else{
    		System.out.println("File: "+filelist[i]);
    		}
    	}
    }

    public int getTotalFolder() {
        return totalFolder;
    }

    public int getTotalFile() {
        return totalFile;
    }
}

